package com.itheima.servise.impl;

import com.itheima.anno.Log;
import com.itheima.controller.exception.BusinessException;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.servise.DeptServise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiseImpl implements DeptServise {
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    // 查询所有
    @Override
    public List<Dept> getAll() {
       return deptMapper.getAll();
    }

    // 删除
    @Log
    @Override
    public void deleteClass(Integer id) {
        //根据id查询部门下有不有员工
        Integer deptId = id;
        Long count = empMapper.selectCountByDeptId(deptId);
        if(count>0){
            throw new BusinessException("该部门下有员工，不能删除");
        }

        deptMapper.deleteClass(id);
    }

    // 添加
    @Log
    @Override
    public void addClass(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.addClass(dept);
    }

    // 根据id查讯
    @Override
    public Dept getById(Integer id) {

        return deptMapper.getById(id);
    }

    // 修改
    @Log
    @Override
    public void putClass(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.putClass(dept);
    }
}
